package com.stfalcon.chatkit.sample.frame;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.stfalcon.chatkit.sample.R;
import com.stfalcon.chatkit.sample.adapter.UserAdapter;
import com.stfalcon.chatkit.sample.common.data.ReceiveListData;
import com.stfalcon.chatkit.sample.common.data.model.User;
import com.stfalcon.chatkit.sample.task.AsyncRquest;
import com.stfalcon.chatkit.sample.url.URLConstant;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class UserFrame extends Fragment {

    TextView userName;

    private RecyclerView recyclerView;

    private  Context context;

    private List<User> userList;

    private SharedPreferences sharedPreferences;

    private UserAdapter userAdapter;

    public UserFrame(Context context)
    {
        this.context = context;
    }




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle bundle) {

        return LayoutInflater.from(context).inflate(R.layout.activity_user_recyclerview, null, false);

    }

    @Nullable
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {


        super.onViewCreated(view, savedInstanceState);
        this.recyclerView = view.findViewById(R.id.recyclerView);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(context));

        userList = new ArrayList<User>();

        userAdapter = new UserAdapter(context, userList);

        this.recyclerView.setAdapter(userAdapter);

        sharedPreferences =  context.getSharedPreferences("myinfo", Context.MODE_PRIVATE);

        this.initView(view);
    }

    private void initView(View view) {
//       this.setTitle(R.string.user_list);
//
//        this.userName = findViewById(R.id.mobileTextView);
//
//        this.userName.setText("里面");

         new Thread(() -> {
             AsyncRquest task = new AsyncRquest(URLConstant.FRIEND_LIST, "token="+sharedPreferences.getString("token", ""));

             AsyncTask<Object, Object, String> res = task.execute();

             try {
                 String jsonString = res.get();

                 Gson son = new Gson();

                 ReceiveListData data = son.fromJson(jsonString, ReceiveListData.class);

                 if (0 == data.getStatus()) {
                     Toast.makeText(context, data.getMessage(), Toast.LENGTH_LONG).show();
                     return;
                 }

                 List<User> tmp = data.getData();

                 System.out.println("tmp:"+ tmp);
                 userList.clear();

                 Iterator<User> iterator = tmp.iterator();

                 while (iterator.hasNext()) {
                     userList.add(iterator.next());
                 }

                 this.userAdapter.setItems(userList);

             } catch (Exception e) {
                 System.out.println("error" + e.getMessage());
             }
         }).run();


    }
}
